package com.purengage.ipilih.lib;

import com.purengage.ipilih.AddResponden;
import com.purengage.ipilih.R;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class DialogBox extends AlertDialog {

	ContextThemeWrapper ctw = new ContextThemeWrapper(getContext(),
			R.style.ThemeDialog);
	AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
	LayoutInflater inflater = getLayoutInflater();
	String strCheck = "";
	DBClass db;

	public DialogBox(Context context) {
		super(context);
		db = new DBClass(context);
	}

	public void PoupupDialog(String title, String message) {
		View view = inflater.inflate(R.layout.dialog_customtitle, null);
		TextView tx = (TextView) view.findViewById(R.id.text_title);
		tx.setText(title);
		builder.setCustomTitle(view).setMessage(message)
				.setNegativeButton("OK", new DialogInterface.OnClickListener() {
					@TargetApi(11)
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				}).create().show();

	}

	public void radioFieldPopup(final String title, final String qid, String[] q) {
		final View formElementView = inflater.inflate(R.layout.popup_pilihan,
				null);
		AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
		final View view = inflater.inflate(R.layout.dialog_customtitle, null);
		TextView tx = (TextView) view.findViewById(R.id.text_title);
		tx.setText(title);

		final RadioGroup pilihan = (RadioGroup) formElementView
				.findViewById(R.id.RadioGroup);

		for (int i = 0; i < q.length; i++) {
			RadioButton rdbtn = new RadioButton(getContext());
			rdbtn.setId(i);
			rdbtn.setText(q[i]);
			pilihan.addView(rdbtn);
		}
		
		builder.setView(formElementView).setCustomTitle(view)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						int selectId = pilihan.getCheckedRadioButtonId();

						RadioButton selectedRadio = (RadioButton) formElementView
								.findViewById(selectId);	
						
						Tmp t = new Tmp();
						t.setName(title);
						t.setQid(qid);
						t.setAnswer(selectedRadio.getText().toString());												
						
						if(db.checkTmpByQid(qid))
							db.deteteTmpByQid(qid);
						
						db.insertTmp(t);						
						dialog.cancel();
					}
				}).create().show();
		
	}

	public void checkFieldPopup(final String title, final String qid, final String[] q) {
		final View formElementView = inflater.inflate(R.layout.popup_pilihan3,
				null);
		AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
		final View view = inflater.inflate(R.layout.dialog_customtitle, null);
		TextView tx = (TextView) view.findViewById(R.id.text_title);
		tx.setText(title);

		final LinearLayout ll = (LinearLayout) formElementView
				.findViewById(R.id.CheckGroup);

		for (int i = 0; i < q.length; i++) {
			CheckBox cb = new CheckBox(getContext());
			cb.setId(i);
			cb.setText(" " + q[i]);
			cb.setOnClickListener(getOnClickDoSomething(cb));
			ll.addView(cb);
		}
		
		builder.setView(formElementView).setCustomTitle(view)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Tmp t = new Tmp();
						t.setName(title);
						t.setQid(qid);
						t.setAnswer(strCheck);
						
						if(db.checkTmpByQid(qid))
							db.deteteTmpByQid(qid);
						
						db.insertTmp(t);
						dialog.cancel();
					}
				}).create().show();
		

	}

	private android.view.View.OnClickListener getOnClickDoSomething(final CheckBox cb) {
		return new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {				
				String ss = (strCheck.equals("")?"":",");
				strCheck += ss+cb.getText().toString().trim(); 
				
			}
		};
	}

	public void textFieldPopup(final String title, final String qid) {
		final View formElementView = inflater.inflate(R.layout.popup_textfield,
				null, false);
		final View vi = inflater.inflate(R.layout.dialog_customtitle, null);
		TextView tx = (TextView) vi.findViewById(R.id.text_title);
		tx.setText(title);

		final EditText TextField = (EditText) formElementView
				.findViewById(R.id.popupText);

		
		builder.setView(formElementView).setCustomTitle(vi)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Tmp t = new Tmp();
						t.setName(title);
						t.setQid(qid);
						t.setAnswer(TextField.getText().toString());
						
						if(db.checkTmpByQid(qid))
							db.deteteTmpByQid(qid);
						
						db.insertTmp(t);
						
						dialog.cancel();												
					}
				}).create().show();
		
		
	}

	public void DatePopup(String title) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
		View vi = inflater.inflate(R.layout.dialog_customtitle, null);
		TextView tx = (TextView) vi.findViewById(R.id.text_title);
		tx.setText(title);
		View view = inflater.inflate(R.layout.popup_datepicker, null, false);
		final DatePicker myDatePicker = (DatePicker) view
				.findViewById(R.id.myDatePicker);
		builder.setView(view).setCustomTitle(vi)
				.setPositiveButton("Go", new DialogInterface.OnClickListener() {
					@TargetApi(11)
					public void onClick(DialogInterface dialog, int id) {
						int month = myDatePicker.getMonth() + 1;
						int day = myDatePicker.getDayOfMonth();
						int year = myDatePicker.getYear();

						Toast.makeText(
								getContext(),
								"You've Selected :" + month + "/" + day + "/"
										+ year, Toast.LENGTH_SHORT).show();
						dialog.cancel();

					}
				}).create().show();

		myDatePicker.setCalendarViewShown(false);
	}

}
